package com.jasonm.rssminer.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class GenerateOutput {
	
	@Autowired
	@Qualifier("objectMapper")
	ObjectMapper mapper;

	static Logger log = Logger.getLogger(GenerateOutput.class);
	
	public String mapToJSON(List<?> objects){
		
		String result = null;
		try {
			result = mapper.writeValueAsString(objects);
		} catch (JsonGenerationException e) {
			log.error("Jackson Error"+e.getMessage());
		} catch (JsonMappingException e) {
			log.error("Jackson Error"+e.getMessage());
		} catch (IOException e) {
			log.error("Jackson Error"+e.getMessage());
		}
		
		
		return result;
	}
	
	public String mapToJSON(Object object){
		List<Object> objs = new ArrayList<Object>();
		objs.add(object);
		return mapToJSON(objs);
	}
	
}
